package com.soulgame.sms.pay;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;

import com.soul.sdk.SGProxy;
import com.soul.sdk.constants.ResultCode;
import com.soul.sdk.plugin.pay.IPayCallBack;
import com.soul.sdk.plugin.pay.PayParams;
import com.soul.sdk.utils.SGDebug;
import com.soul.sdk.utils.ToastUtil;
import com.soul.sdk.utils.json.KJSONObject;
import com.soulgame.sms.contant.PayType;
import com.soulgame.sms.pay.vo.VoPayParams;
import com.soulgame.sms.pay.vo.VoWoShopPayParams;
import com.unicom.dcLoader.Utils;
import com.unicom.dcLoader.Utils.UnipayCommandResultListener;
import com.unicom.dcLoader.Utils.UnipayPayResultListener;

/**
 * @author lch
 * @version Create time：2017-3-15
 * @类说明：联通 沃商店
 */
public class WoShopPay extends SMSPay {

	private Activity mActivity;
	private VoWoShopPayParams mVoWoShopPayParams;

	@Override
	public String getPayType() {
		return PayType.WOSHOP;
	}

	@Override
	public VoPayParams getVoPayParams() {
		return mVoWoShopPayParams;
	}

	@Override
	public void init(Activity pActivity, KJSONObject pSdkParamsJSON) {
		this.mActivity = pActivity;
		this.mVoWoShopPayParams = new VoWoShopPayParams(pSdkParamsJSON);
	}

	@Override
	public void pay(Activity pActivity, PayParams pPayParams, IPayCallBack pPayCallBack) {

		if (!isInitOk()) {
			ToastUtil.showShort(pActivity, "WoShop has not been initialized");
			// 未初始化完成
			pPayCallBack.onFail(ResultCode.CODE_INIT_FAIL, "未正常初始化", pPayParams);
			return;
		}

		boolean isOnlineGame = SGProxy.getInstance().isOnlineGame();
		String aProductId = pPayParams.getProductId();

		// 3位计费点编号，不可为null
		String payCode = mVoWoShopPayParams.getPayCode(aProductId);

		if (isOnlineGame) {
			// TODO 联网支付还需要服务端对接 校验
			// (Context mContext, String number, String monthType, String
			// orderid,UnipayPayResultListener listener);
			// 支付订单号，24位，不可为null
			String aCustomOrderId = pPayParams.getSdkOrderId();
			// "0"道具关卡等,"1"按次代缴订购,"2"按次代缴退订
			String aMonthType = mVoWoShopPayParams.getMonthType(aProductId);
			Utils.getInstances().payOnline(pActivity, payCode, aMonthType, aCustomOrderId,
					getListener(pPayParams, pPayCallBack));
		} else {
			Utils.getInstances().pay(pActivity, payCode, getListener(pPayParams, pPayCallBack));
		}

	}

	/**
	 * 包月
	 * 
	 * @param product
	 */
	private void monthSubscibe(Activity pActivity, PayParams pPayParams, IPayCallBack pPayCallBack) {
		// 产品资费：10元/月
		try {
			JSONObject object = new JSONObject();
			// command只能为month_order_10
			object.put("command", "month_order_10");
			Utils.getInstances().customCommand(pActivity, object.toString(),
					getUnipayCommandResultListener(pPayParams, pPayCallBack));
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private UnipayCommandResultListener getUnipayCommandResultListener(final PayParams pPayParams,
			final IPayCallBack pPayCallBack) {
		return new UnipayCommandResultListener() {
			@Override
			public void CommandResult(String content) {
				/**
				 * 返回结果content以下三种： {"result": "1","msg":"兑换成功"} {"result":
				 * "2","msg":"兑换失败"} {"result": "3","msg":"用户取消"}
				 */
				int result = 0;
				if (content != null) {
					KJSONObject json = KJSONObject.createJsonObject(content);
					if (json != null) {
						result = json.getInt("result", 0);
					}
				}
				switch (result) {
				case 1:
					if (pPayCallBack != null) {
						pPayCallBack.onSuccess(ResultCode.CODE_PAY_SUCCESS, "包月成功", pPayParams);
					}
					break;
				case 2:
					if (pPayCallBack != null) {
						pPayCallBack.onFail(ResultCode.CODE_PAY_FAIL, "支付失败", pPayParams);
					}
					break;

				case 3:
					if (pPayCallBack != null) {
						pPayCallBack.onFail(ResultCode.CODE_PAY_CANCEL, "支付取消", pPayParams);
					}
					break;

				default:
					if (pPayCallBack != null) {
						pPayCallBack.onFail(ResultCode.CODE_PAY_FAIL, "支付失败", pPayParams);
					}
					break;
				}
			}
		};
	}

	private UnipayPayResultListener getListener(final PayParams pPayParams, final IPayCallBack pPayCallBack) {
		return new UnipayPayResultListener() {

			@Override
			public void PayResult(String paycode, int flag, int flag2, String error) {
				// flag为支付回调结果，flag2为回调类型，error为当前结果描述，paycode是完整的计费点编码
				switch (flag) {
				case 1:// success
						// 此处放置支付请求已提交的相关处理代码
					if (pPayCallBack != null) {
						pPayCallBack.onSuccess(ResultCode.CODE_PAY_SUCCESS, "支付成功", pPayParams);
					}
					break;

				case 2:// fail
						// 此处放置支付请求失败的相关处理代码
					if (pPayCallBack != null) {
						pPayCallBack.onFail(ResultCode.CODE_PAY_FAIL, "支付失败", pPayParams);
					}
					SGDebug.print_w(WoShopPay.this, error);
					break;

				case 3:// cancel
						// 此处放置支付请求被取消的相关处理代码
					if (pPayCallBack != null) {
						pPayCallBack.onFail(ResultCode.CODE_PAY_CANCEL, "支付取消", pPayParams);
					}
					break;

				default:
					if (pPayCallBack != null) {
						pPayCallBack.onFail(ResultCode.CODE_PAY_FAIL, "支付结果未知", pPayParams);
					}
					SGDebug.print_w(WoShopPay.this, error);
					break;
				}
			}
		};

	}

	/**
	 * 检测SDK是否正常初始化
	 * 
	 * @本章提到的所有接口中，初始化接口最为重要。其他接口调用必须等到初始化完成后进行调用方可。
	 * 
	 * @return
	 */
	public boolean isInitOk() {

		boolean isInit = false;

		try {
			isInit = Utils.getInstances().isInit();
		} catch (Exception e) {
			SGDebug.print_w(this, e.toString());
		}
		return isInit;
	}

	@Override
	public void onResume() {
		// 联通统计相关，（可选）
		if (isInitOk()) {
			Utils.getInstances().onResume(mActivity);
		}
	}

	@Override
	public void onPause() {
		// 联通统计相关，（可选）
		if (isInitOk()) {
			Utils.getInstances().onPause(mActivity);
		}
	}

}
